﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SQLTouch.Service.DataContracts
{
    [DataContract]
    [KnownType(typeof(QueryResultColumnData<int>))]
    [KnownType(typeof(QueryResultColumnData<float>))]
    [KnownType(typeof(QueryResultColumnData<double>))]
    [KnownType(typeof(QueryResultColumnData<string>))]
    [KnownType(typeof(QueryResultColumnData<DateTime>))]
    public abstract class QueryResultColumnData
    {
        [DataMember]
        private readonly string name;

        protected QueryResultColumnData()
        {
        }

        public QueryResultColumnData(string name)
        {
            this.name = name;
        }

        public string Name
        {
            get { return name; }
        }
    }

    [DataContract]
    public class QueryResultColumnData<T> : QueryResultColumnData
    {
        [DataMember]
        private readonly List<T> values;
        private ReadOnlyCollection<T> valuesReadOnly;

        private QueryResultColumnData()
        {
        }

        public QueryResultColumnData(string name, IEnumerable<T> values)
            : base(name)
        {
            this.values = values.ToList();
        }

        public ReadOnlyCollection<T> Values
        {
            get
            {
                if (valuesReadOnly == null)
                {
                    valuesReadOnly = new ReadOnlyCollection<T>(values);
                }
                return valuesReadOnly;
            }
        }
    }
}
